$(function () {

    layui.use(['form','layer'], function(){
        $ = layui.jquery;
        var form = layui.form
            ,layer = layui.layer;

        //自定义验证规则
        form.verify({
            name: function(value){
                if(value == ''){
                    return '角色名不能为空';
                }
            }
        });

        //监听提交
        form.on('submit(add)', function(data){
            //console.log(data);
            //发异步，把数据提交给php
            $.ajax({
                url:save_url,
                data:data.field,
                type:"Post",
                dataType:"json",
                success:function(data){
                    if(data.status == 1){
                        layer.msg("添加成功");
                        var index = parent.layer.getFrameIndex(window.name);
                        //关闭当前frame
                        parent.layer.close(index);
                        parent.location.reload();
                    }
                },
                error:function(data){
                    layer.msg("服务器无响应");
                }
            });

            return false;
        });
    });

});


/**
 * 启用 / 禁用
 */
function handle_able(obj, id) {
    var title = $(obj).attr('title');
    layer.confirm('确认要'+title+'吗？',function(index){
        var type = $(obj).attr('data-type');
        $.ajax({
            url: handle_url,
            data: {"id": id,'type':type},
            type: "POST",
            dataType: "json",
            success: function (data) {
                if (data.status == 1) {
                    if(title == '启用'){
                        // 启用
                        $(obj).attr('type','2');
                        $(obj).attr('title','禁用');
                        $(obj).find('i').html('&#xe62f;');

                        //$(obj).parents("tr").find(".td-status").find('span').html('已启用');
                        layer.msg('已启用!',{icon: 5,time:1000});
                    }else{
                        // 禁用
                        $(obj).attr('type','1');
                        $(obj).attr('title','启用');
                        $(obj).find('i').html('&#xe601;');

                        //$(obj).parents("tr").find(".td-status").find('span').html('已禁用');
                        layer.msg('已禁用!',{icon: 5,time:1000});
                    }
                    setTimeout(function(){
                        window.location.reload();
                    }, 1000);
                } else {
                    layer.msg(data.message);
                }
            },
            error: function (data) {
                layer.msg("服务器无响应");
            }
        });
    });
}